// 来源 echarts/lib/util/format

/**
 * [Numberic is defined as]:
 *     `parseFloat(val) == val`
 * For example:
 * numeric:
 *     typeof number except NaN, '-123', '123', '2e3', '-2e3', '011', 'Infinity', Infinity,
 *     and they rounded by white-spaces or line-terminal like ' -123 \n ' (see es spec)
 * not-numeric:
 *     null, undefined, [], {}, true, false, 'NaN', NaN, '123ab',
 *     empty string, string with only white-spaces or line-terminal (see es spec),
 *     0x12, '0x12', '-0x12', 012, '012', '-012',
 *     non-string, ...
 *
 * @test See full test cases in `test/ut/spec/util/number.js`.
 * @return Must be a typeof number. If not numeric, return NaN.
 */
function numericToNumber(val: number | string) {
    const valFloat = parseFloat(val as string);
    return valFloat == val && (valFloat !== 0 || typeof val !== "string" || val.indexOf("x") <= 0) // For case ' 0x0 '.
        ? valFloat
        : NaN;
}

export default function (x: number | string) {
    if (isNaN(numericToNumber(x))) {
        return typeof x === "string" ? x : "-";
    }

    const parts = (x + "").split(".");
    return parts[0].replace(/(\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,") + (parts.length > 1 ? "." + parts[1] : "");
}
